<?php
/**
 * 管理员模块
 **/
namespace Mama\Controller;
use Think\Controller;

class CosttypeController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(!session('userid')){ //已登陆
			$this->redirect("index/index");
		}
    }
	
	public function index(){
		if(IS_GET){
			$ajax = I('ajax') ? (int)I('ajax') : 0;
			$page = I('page', 1);
			//获取总数
			$count = D('Eventtype')->getEventTypeListCount($where);
			//获取列表
			$list = D('Eventtype')->getEventTypeList($where);
			//编列集合
			foreach($list as $ls){
				$newEvent[$ls['typeid']]['typeid'] = $ls['typeid'];
				$newEvent[$ls['typeid']]['type_title'] = $ls['type_title'];
				$newEvent[$ls['typeid']]['type_btime'] = $ls['type_btime'];
				$newEvent[$ls['typeid']]['type_etime'] = $ls['type_etime'];
				$newEvent[$ls['typeid']]['type_status'] = $ls['type_status'];
				$newEvent[$ls['typeid']]['admin_nick'] = $ls['admin_nick'];
				$newEvent[$ls['typeid']]['createtime'] = $ls['createtime'];
				$newEvent[$ls['typeid']]['attr'][] = array(
					'remark' => $ls['remark']
				);
			}

			$this->assign('list', $newEvent);
			$this->assign('count', $count);
			$this->assign('page', $page);
			if($ajax){
				$this->display('list', 'utf-8', 'html');
			}else{
				$this->display();
			}
		}
	}
	
	/**
	 * 添加收费类型
	 **/
	public function add(){
		if(IS_GET){
			//获取详情
			$cost_id = I('typeid') ? (int)I('typeid') : 0;
			if($cost_id){
				$costinfo = D('Eventtype')->getCostTypeInfoByTypeId($cost_id);
				$this->assign('info', $costinfo);
				//获取规则
				$attrlist = D('Eventtype')->getEventTypeAttrByEventId($cost_id);
				$this->assign('attrlist', $attrlist);
				$attrcount = count($attrlist);
			}

			$attrcount = $attrcount ? $attrcount : 1;
			$this->assign('attrcount', $attrcount);
			//获取房间类型
			$roomtype = D('Room')->getRoomTypeList();
			$this->assign('roomtype', $roomtype);
			$this->display();
		}else{
			$data = I('post.');
			$data['admin_id'] = session('userid');
			$data['admin_nick'] = session('nickname');
			$data['createtime'] = time();
			
			if(empty($data['type_title'])){
				$this->ajaxReturn('请输入活动标题');
			}else{
				$data['type_btime'] = strtotime($data['type_btime']);
				$data['type_etime'] = strtotime($data['type_etime']) + 86399;
				//开启事物
				D('Eventtype')->startTrans();
				
				if(empty($data['typeid'])){
					$row = D('Eventtype')->addEventTypeInfo($data);
					if($row){
						//开始组装规则
						foreach($data['param'] as $ls){
							$ls['event_id'] = $row;
							$ls['other'] = empty($ls['other']) ? 0 : (int)$ls['other'];
							//编辑房间类型
							$roomtype = $ls['room_type'];
							if(!empty($roomtype)){
								$array = explode('|', $roomtype);
								$ls['room_type'] = $array[1];
								$ls['roomtypeid'] = $array[0];
							}
							$ls['remark'] = $this->attrString($ls, $ls['room_type']);
							$newAttr[] = $ls;
						}

						//保存规则
						$rs = D('Eventtype')->addEventTypeAttr($newAttr);
						if($rs){
							//提交事物
							D('Eventtype')->commit();
						}else{
							D('Eventtype')->rollback();
						}
					}else{
						D('Eventtype')->rollback();
					}
				}else{
					$cost_id = $data['typeid'];
					unset($data['typeid']);
					$row = D('Eventtype')->editEventTypeInfoByTypeId($cost_id, array(
						'type_title' => $data['type_title'],
						'type_btime' => $data['type_btime'],
						'type_etime' => $data['type_etime'],
						'contract' => $data['contract']
					));

					if($row !== false){
						//删除规则
						$rs = D('Eventtype')->deleteEventTypeAttrByEventId($cost_id);
						//开始组装规则
						foreach($data['param'] as $ls){
							$ls['event_id'] = $cost_id;
							$ls['other'] = empty($ls['other']) ? 0 : (int)$ls['other'];
							$roomtype = $ls['room_type'];
							if(!empty($roomtype)){
								$array = explode('|', $roomtype);
								$ls['room_type'] = $array[1];
								$ls['roomtypeid'] = $array[0];
							}
							$ls['remark'] = $this->attrString($ls, $ls['room_type']);
							$newAttr[] = $ls;
						}

						if($rs !== false){
							//保存新的规则
							D('Eventtype')->addEventTypeAttr($newAttr);
							//提交事物
							D('Eventtype')->commit();
						}else{
							//回滚事物
							D('Eventtype')->rollback();
						}
						
					}else{
						//回滚事物
						D('Eventtype')->rollback();
					}
				}
				
				if($row !== false){
					$this->ajaxReturn('编辑成功');
				}else{
					$this->ajaxReturn('编辑失败');
				}
			}
		}
	}
	
	public function edit(){
		$typeid = I('typeid', 0);
		$status = I('status', 0);
		if(D('Eventtype')->editEventTypeInfoByTypeId($typeid, array('type_status' => $status)) !== false){
			echo 1;
		}else{
			echo 0;
		}
	}
	//删除规则
	public function remove(){
		$typeid = I('typeid') ? (int)I('typeid') : 0;
		if(D('Eventtype')->deleteEventInfoById($typeid) !== false){
			$this->ajaxReturn('删除成功');
		}else{
			$this->ajaxReturn('删除失败');
		}
	}
	
	//组装活动说明
	public function attrString($array=array(), $roomtype){
		$remark = '['. $roomtype .'房型]签约';
		//签约6个月以上：减免物业管理费用一半；补贴50元水电/月
		switch($array['symbol']){
			case 0:
				$remark .= '不满';
			break;
			case 1:
				$remark .= '满';
			break;
			case 2:
				$remark .= '超过';
			break;
			case 3:
			break;
			case 4:
			break;
		}
		$remark .= $array['attr_month']. '个月';
		//描述月租
		if($array['rent_count']){
			$zk = $array['rent_multiple'] == 0 ? '全免' : $array['rent_multiple']*10 . '折';
			$remark .= '享受房租最后'. $array['rent_count'].'个月('. $zk .');';
		}
		//描述物业
		if($array['property_count']){
			$zk = $array['property_multiple'] == 0 ? '全免' : $array['property_multiple']*10 . '折';
			if($array['property_count'] == -1){
				$remark .= '物业费租期内('. $zk .')';
			}else{
				$remark .= '物业费'. $array['property_count'] .'个月('. $zk .');';
			}
		}
		//水电补助
		if($array['other'] > 0){
			$remark .= '补贴不超过'. $array['other'] . '元水电/月;';
		}

		return $remark;
	}
}

